//
//  WorkStateViewController.m
//  SouthChina
//
//  Created by kc on 15/7/9.
//  Copyright (c) 2015年 科创. All rights reserved.
//我的工作状态

#import "WorkStateViewController.h"
#import "DataDictDao.h"
#import "PersonalCenterTableViewCell.h"

@interface WorkStateViewController ()<UITableViewDataSource,UITableViewDelegate>{
    DataDict *dicttype;
}
@property (strong, nonatomic) IBOutlet UIImageView *imagehead;
@property (strong, nonatomic) IBOutlet UITableView *tabview;
@property (strong, nonatomic) NSArray * titles; //标题列表
@property (strong, nonatomic) IBOutlet UILabel *userstate;//用户状态
@property (strong, nonatomic) IBOutlet UILabel *username;//用户名
@property NSInteger currentIndex;//当前选中索引值
@end

@implementation WorkStateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addTitleBarWithBackBtn:@"我的工作状态" needReturn:YES];
    UIView * viewBody = [[[NSBundle mainBundle] loadNibNamed:@"WorkStateViewBody" owner:self options:nil] objectAtIndex:0];
    viewBody.frame = (CGRect){0, self.titlebar.frame.origin.y+self.titlebar.frame.size.height,SCREEN_WIDTH, SCREEN_HEIGHT-self.titlebar.frame.size.height};
    [self.view addSubview:viewBody];
    
    _tabview.delegate = self;
    _tabview.dataSource = self;
    
    //用户头像
    NSString *headStr = [DE getGlobalVar:LOGIN_USER_HEADIMG] == nil?@"":[DE getGlobalVar:LOGIN_USER_HEADIMG];
    NSURL *url= [NSURL URLWithString: [headStr hasPrefix:@"http"]?headStr:[NSString stringWithFormat:@"%@%@",DE.fileEngineeUrl,headStr]];
    
    [_imagehead setImageWithURL:url placeholderImage:[UIImage imageNamed:@"empty_photo"]];
    _imagehead.layer.masksToBounds = YES;
    _imagehead.layer.cornerRadius = 24;
    
    //用户名
    //获取登陆用户名
    NSString * strName = [NSString stringWithFormat:@"%@(%@)",[DE getGlobalVar:LOGIN_USER_NAME] == nil?@"":[DE getGlobalVar:LOGIN_USER_NAME],[[DE getGlobalVar:LOGIN_USER_TYPE] isEqualToString:@"T"]?@"教职工":@"学生"];
    NSLog(@"用户类型：%@。。。。。%@",[DE getGlobalVar:LOGIN_USER_TYPE],strName);
    _username.text = strName;
    
    //读取用户状态
    NSString *userstate = [DE getGlobalVar:LOGIN_USER_WORKSTATUS] == nil?@"":[DE getGlobalVar:LOGIN_USER_WORKSTATUS];
    
    //查询工作状态数据字典
    _currentIndex = -1;
    _userstate.text = userstate;
    NSString *sql = [NSString stringWithFormat:@"where dicttypeId ='7'" ];
    _titles = [DataDictDao queryDataDict:sql];
    
    if(![userstate isEqualToString:@""])
    {
        for(NSInteger i = 0;i<_titles.count;i++)
        {
            DataDict *dict =[_titles objectAtIndex:i];
            if ([userstate isEqualToString:dict.dictdataValue] ) {
                _currentIndex = i;
                dicttype = dict;
                break;
            }
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//返回分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 26;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0001;//不要返回0，返回0的话，设置无效果
}

//每个分区多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _titles.count;
}



//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 56;
}

//设置每行的显示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"PersonalCenterTableViewCell";
    PersonalCenterTableViewCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
    }
    DataDict *dict =[_titles objectAtIndex:indexPath.row];
    cell.titleLabel.text = dict.dictdataValue;
    cell.labelconstraint.constant= 5;
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //选中后显示选中图像
    if(_currentIndex == indexPath.row )
    {
        UIImageView * aseryImage = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-25, 25, 12, 10)];
        aseryImage.image = [UIImage imageNamed:@"单选选中.png"];
        [cell addSubview:aseryImage];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _currentIndex = indexPath.row;
    //刷新列表
    [_tabview reloadData];
    dicttype =[_titles objectAtIndex:indexPath.row];
    //更新工作状态
    _userstate.text  = dicttype.dictdataValue;
    
}

//回退时才提交
- (void)btnBack {
    //更新到工作状态
    NSDictionary *map = @{@"workStatus":dicttype.dictdataValue};
    [DE serverCall:@"updateUserInfo" params:map callback:^(BOOL success, int code, NSString *desc, NSDictionary *data,NSDictionary*param) {
     NSString *sReturnInfo = @"";
     if(success)
     {
        [DE setGlobalVar:LOGIN_USER_WORKSTATUS value:dicttype.dictdataValue];
        sReturnInfo = @"工作状态修改成功";
      }
     else{
        sReturnInfo = @"工作状态修改失败";
         }
        [[NSNotificationCenter defaultCenter] postNotificationName:CHANGE_WORK_STATUS object:sReturnInfo];
        [super btnBack];
     }];
}

@end
